﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;
using SHOP.Areas.Admin.Models;

namespace SHOP.Areas.Admin.Controllers
{
    public class PropertiesController : Controller
    {
        //
        // GET: /Admin/Maker/

        //Kết nối dữ liệu
        ProductModelDataContext dataTable = new ProductModelDataContext();

        public Constants Constants = new Constants();
        public string NameController = "properties";

        [HandleError]
        public ActionResult Index()
        {
            //Delete Multi Record
            string[] items = Request.Form.GetValues("items");

            if (items != null)
            {
                foreach (var value in items)
                {
                    var ItemAccount = dataTable.properties.Single(b => b.id == int.Parse(value));
                    dataTable.properties.DeleteOnSubmit(ItemAccount);
                    dataTable.SubmitChanges();
                }

                return Redirect("/admin/" + NameController + "#success");
            }

            return View();
        }

        [HandleError, HttpPost]
        public ActionResult LoadContent()
        {
            string Sort_By = "";
            string Sort_Type = "";
            string Search_Type = "name";
            string KeyWord = Request.Form["keyword"];

            string[] Columns = { "name", "phone", "status", "created", "changed" };
            string sortby = Array.Find(Columns, delegate(string s) { return s == Request.Form["sort_by"]; });

            string[] Sort = { "asc", "desc" };
            string sorttype = Array.Find(Sort, delegate(string s) { return s == Request.Form["sort_type"]; });

            if (sortby != null) { Sort_By = sortby; } else { Sort_By = "id"; };
            if (sorttype != null) { Sort_Type = sorttype; } else { Sort_Type = "desc"; };

            //Pagination
            var page = int.Parse(Request.Form["page"]);
            if (KeyWord != "")
            {
                var ListItem = (from u in dataTable.properties select u).Where(Search_Type + ".Contains(@0)", KeyWord).OrderBy(Sort_By + " " + Sort_Type).Skip((page - 1) * Constants.pageSize).Take(Constants.pageSize).ToList();
                ViewBag.numPage = (from u in dataTable.properties select u).Where(Search_Type + ".Contains(@0)", KeyWord).Count();
                ViewBag.View = ListItem;

            }
            else
            {
                var ListItem = (from u in dataTable.properties select u).Where("status != @0", 2).OrderBy(Sort_By + " " + Sort_Type).Skip((page - 1) * Constants.pageSize).Take(Constants.pageSize).ToList();
                ViewBag.numPage = (from u in dataTable.properties select u).Where("status != @0", 2).Count();
                ViewBag.View = ListItem;
            }

            ViewBag.Page = page;
            ViewBag.Keyword = KeyWord;
            ViewBag.Sort_By = Sort_By;
            ViewBag.Sort_Type = Sort_Type;
            ViewBag.pageSize = Constants.pageSize;

            return View();
        }

        [HandleError]
        public ActionResult Update()
        {
            if (Request.QueryString["id"] != null && System.Text.RegularExpressions.Regex.IsMatch(Request.QueryString["id"], @"\d"))
            {
                var id = Convert.ToInt32(Request.QueryString["id"]);
                var ListItem = dataTable.properties.SingleOrDefault(b => b.id == id);
                ViewBag.View = ListItem;
            }
            return View();
        }

        [HandleError, HttpPost]
        public ActionResult Save()
        {
            var json = "";

            if (Request.Form["id"] == "")
            {
                //Insert
                if (ModelState.IsValid)
                {
                    var ListItem = new property();

                    ListItem.name = Request.Form["name"];
                    ListItem.slug = Request.Form["slug"];
                    ListItem.status = (Request.Form["status"] != null) ? 1 : 0;
                    ListItem.changed = DateTime.Now;
                    ListItem.created = DateTime.Now;

                    dataTable.properties.InsertOnSubmit(ListItem);
                    dataTable.SubmitChanges();

                    json = "success";
                }
                else
                {
                    json = "fail";
                }

            }
            else
            {

                //Update
                var id = int.Parse(Request.Form["id"]);
                int recCount = dataTable.properties.Count(b => b.id == id);

                if (ModelState.IsValid && recCount > 0)
                {
                    var ListItem = dataTable.properties.Single(b => b.id == id);
                    ListItem.name = Request.Form["name"];
                    ListItem.slug = Request.Form["slug"];
                    ListItem.status = (Request.Form["status"] != null) ? 1 : 0;
                    ListItem.changed = DateTime.Now;
                    dataTable.SubmitChanges();

                    json = "success";
                }
                else
                {
                    json = "fail";
                }
            }

            return Json(new { st = json });
        }

        [HandleError, HttpPost]
        public ActionResult Status()
        {
            //Update
            var json = "";

            int id = Convert.ToInt32(Request.Form["id"]);

            int recCount = dataTable.properties.Count(b => b.id == id);
            if (recCount > 0)
            {
                var ListItem = dataTable.properties.Single(b => b.id == id);
                ListItem.status = int.Parse(Request.Form["status"]);
                dataTable.SubmitChanges();
                json = "success";
            }
            else
            {
                json = "fail";
            }

            return Json(new { st = json });
        }

    }
}
